﻿using BW.EatRabbit.API.Application.Commands.SlaughterCompany;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.SlaughterCompany
{
    public class CompanyQueryCommandHandler : IRequestHandler<CompanyQueryCommand, APIResult<Slaughter_Company>>
    {
        private readonly IBaseRepository<Slaughter_Company> _companyRepository;
        public CompanyQueryCommandHandler(IBaseRepository<Slaughter_Company> companyRepository)
        {
            _companyRepository = companyRepository;
        }
        public Task<APIResult<Slaughter_Company>> Handle(CompanyQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Slaughter_Company> result = new APIResult<Slaughter_Company>();

            var list= _companyRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
            if (list == null)
            {
                result.Code = ResultCode.查询失败;
                result.Message = ResultCode.查询失败.ToString();

            }
            else
            {
                result.Code = ResultCode.查询成功;
                result.Message = ResultCode.查询成功.ToString();
                result.Data = list;
            }
            return Task.FromResult(result);
        }
    }
}
